package com.org.khtn.command.select;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.org.khtn.command.MyParse;
import com.org.khtn.command.QueryPattern;
import com.org.khtn.stringcommand.Format;
import com.org.khtn.ultilities.Ultilities;

public class ParseSelect extends MyParse {
	Map<Integer, String> data;

	public ParseSelect(String Query) {
		query = Query;
	}

	public void putData(int i, String tam) {
		data.put(i, tam);
	}

	@Override
	public String[] parse() {
		if (!eval())
			return null;
		String commandString = format();
		commandString = commandString.toLowerCase();
		// String select;
		// String object = "(\\D\\w*)";
		// String tableOrField = "(" + object + ")|((" + object + "),)*" +
		// object;
		//
		// String selectPattern = "\\s*+(?i)select\\s+(\\*|" + tableOrField
		// + "|("+tableOrField+"\\.\\*"+"))\\s*";
		//
		// String[] tam= query.split(selectPattern,2);
		// int length = tam.length;
		//
		// for (String t : tam)
		// {
		// System.out.prrffintln(t);
		// }

		commandString = commandString.replace(", ", ",");
		commandString = commandString.replace("( ", "(");
		commandString = commandString.replace(" )", ")");
		commandString = commandString.replace(" =", "=");
		commandString = commandString.replace("= ", "=");
		String tam = commandString;
		System.out.println(tam);
		int nI = 0;
		data = new HashMap<Integer, String>();
		while (true) {
			String[] pattern = new String[] { "\\(.*\\)\\s+as\\s+\\w+",
					"\\(.*\\)" };
			Vector arr = new Vector<>();
			int length = pattern.length;
			boolean flag = false;
			for (int i = 0; i < length; i++) {
				commandString = commandString.replaceFirst(pattern[i], "|" + nI
						+ "|");
				if (!tam.equalsIgnoreCase(commandString)) {
					int idxBegin = tam.indexOf('(');
					// System.out.println(idxBegin);

					if (idxBegin == -1)
						break;
					int idxEnd = tam.indexOf(") ", idxBegin);
					if (idxEnd == -1)
						idxEnd = tam.length() - 1;
					// System.out.println(idxEnd);

					String temp = tam.substring(idxBegin, idxEnd + 1);
					System.out.println(temp);
					data.put(nI, temp);
					//
					nI++;
					// arr.add(temp);
					System.out.println(arr);

					tam = commandString;
					flag = true;
				}
			}

			if (!flag)
				break;
		}
		int j = 0;
		tam = tam.replace("group by", "groupby");
		System.out.println("tam: " + tam);
		String[] parse = tam.split(" ");
		int length = parse.length;
		for (int i = 0; i < length; i++) {
			if (parse[i].contains("|")) {
				parse[i] = data.get(j);
				j++;
			}
			// System.out.println(parse[i]);

		}
		String[] result = new String[8];
		for (int i = 0; i < 8; i++)
			result[i] = null;
		String[] key = new String[] { "select", "from", "join", "on", "where",
				"groupby", "having", "orderby" };
		Ultilities.print_r(parse);
		for (int i = 1; i < length; i = i + 2) {
			int pos = -1;
			switch (parse[i - 1]) {
			case "select":
				pos = 0;
				break;
			case "from":
				pos = 1;
				break;
			case "join":
				pos = 2;
				break;
			case "on":
				pos = 3;
				break;
			case "where":
				pos = 4;
				break;
			case "groupby":
				pos = 5;
				break;
			case "having":
				pos = 6;
				break;
			case "orderby":
				pos = 7;
				break;

			}
			if (pos != -1)
				result[pos] = parse[i];
		}
		Ultilities.print_r(result);
		return result;
	}

	private String format() {
		Format format = new Format(query);
		format.commandString = format.XLC();
		Vector object = format.getStringMarker();
		Vector object2 = format.getStringMarker();
		Boolean flags = false;
		String commandString = format.commandString;

		int length = object2.size();

		for (int i = 0; i < length; i++) {
			commandString = commandString.replace("|" + i + "|", "("
					+ (String) object2.get(i) + ")");
		}
		length = object.size();
		for (int i = 0; i < length; i++) {
			commandString = commandString.replace("{" + i + "}",
					(String) object.get(i));
		}
		return commandString;
	}

	public void setQuery(String Query) {
		query = Query;
	}

	public String getQuery() {
		return query;
	}

	@Override
	public boolean eval() {
		// TODO Auto-generated method stub
		QueryPattern.createPattern();
		Pattern a = Pattern.compile(QueryPattern.SELECT_PATTERN,
				Pattern.CASE_INSENSITIVE);
		Matcher b = a.matcher(query);

		return b.matches();
	}

}
